﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;


namespace TDME
{
    public partial class NewMap : Form
    {
        Main main;
        public NewMap(Main m)
        {
            InitializeComponent();
            main = m;
        }
       
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (PruefeInput())
            {
                string map = Main.GenerateMap(txtMapname.Text, txtEditor.Text, ushort.Parse(txtWidth.Text), ushort.Parse(txtHeight.Text));
                main.LoadMapFromFile(map);
                this.DialogResult = DialogResult.OK; 
                this.Close();
            }
        }

        private bool PruefeInput()
        {
            bool erg = true;

            lblMapname.ForeColor = Color.Black;
            lblWidth.ForeColor = Color.Black;
            lblHeight.ForeColor = Color.Black;
            lblEditor.ForeColor = Color.Black;

            if (txtEditor.Text.Trim() == "")
            {
                lblEditor.ForeColor = Color.Red;
                erg = false;
            }

            if (txtMapname.Text.Trim() == "")
            {
                lblMapname.ForeColor = Color.Red;
                erg = false;
            }

            if (txtWidth.Text.Trim() == "" || txtWidth.Text == "0")
            {
                lblWidth.ForeColor = Color.Red;
                erg = false;
            }

            if (txtHeight.Text.Trim() == "" || txtWidth.Text == "0")
            {
                lblHeight.ForeColor = Color.Red;
                erg = false;
            }
            return erg;
        }

        private void txtWidth_KeyPress(object sender, KeyPressEventArgs e)
        {
            string wert = e.KeyChar.ToString();

            // Ziffern
            if (Char.IsDigit(e.KeyChar))
            {
                return;
            }
            // Backspace
            else if (e.KeyChar == '\b')
            {
                return;
            }
                        
            e.Handled = true;
        }

        private void txtMapname_KeyPress(object sender, KeyPressEventArgs e)
        {
            string wert = e.KeyChar.ToString();

            // Ziffern
            if (Char.IsLetterOrDigit(e.KeyChar))
            {
                return;
            }
            // Backspace
            else if (e.KeyChar == '\b')
            {
                return;
            }

            e.Handled = true;
        }
    }
}
